(function($)
{
	//If the UI scope is not available, add it
	$.ui = $.ui || {};

	$.fn.dialog = function(o) {
		return this.dialogInit(o).dialogOpen();
	}
	$.fn.dialogInit = function(o) {
		return this.each(function() {
			if (!$(this).is(".ui-dialog-content")) {
				new $.ui.dialogInit(this, o);
			}			
		});
	}
	$.fn.dialogOpen = function() {
		return this.each(function() {
			var contentEl;
			if ($(this).parents(".lightbox").length) contentEl = this;
			if (!contentEl && $(this).is(".lightboxWrapper")){ 
				contentEl = $('.ui-dialog-content', this)[0];
			}
			$.ui.dialogOpen(contentEl)
		});
	}
	$.fn.dialogClose = function() {
		return this.each(function() {
			var contentEl;
			var closeEl = $(this);
			if (closeEl.is('.ui-dialog-content')) {
				var contentEl = closeEl;
			} else if (closeEl.hasClass('lightboxWrapper')) {
				contentEl = closeEl.find('.ui-dialog-content');
			} else {
				contentEl = closeEl.parents('.lightboxWrapper:first').find('.ui-dialog-content');
			}
			$.ui.dialogClose(contentEl[0]);
		});
	}

	//el是对话框的具体内容
	$.ui.dialogInit = function(el, o) {
		var options = {
			width: 500,
			draggable: true,
			overlay : true  //是否添加overlay元素
		};
		var o = o || {}; 
		$.extend(options, o); //Extend and copy options
		this.element = el; 
		var self = this; //Do bindings
		//将当前对象存储于当前dom名为"ui-dialog"的key下
		$.data(this.element, "ui-dialog", this);

		var uiDialogContent = $(el).addClass('ui-dialog-content').show();

		if (!uiDialogContent.parent().length) {
			uiDialogContent.appendTo('body');
		}
		uiDialogContent
			.wrap(document.createElement('div'))
			.wrap(document.createElement('div'));
		var uiDialogContainer = uiDialogContent.parent().addClass('lightbox').css('width', options.width);
		var uiDialog = uiDialogContainer.parent().hide().addClass('lightboxWrapper').css('zIndex', 1001);

		var classNames = uiDialogContent.attr('className').split(' ');

		// Add content classes to dialog, to inherit theme at top level of element
		$.each(classNames, function(i, className) {
			if (className != 'ui-dialog-content')
				uiDialog.addClass(className);
		});

		uiDialogContainer.prepend('<div class="header"></div>');
		var uiDialogTitlebar = $('.header', uiDialogContainer);
		var title = (options.title) ? options.title : (uiDialogContent.attr('title')) ? uiDialogContent.attr('title') : '';
		uiDialogTitlebar.append('<a class="close" href="#">X</a>');
		uiDialogTitlebar.append('<div style="font-size: 16px; font-weight: bold;">' + title + '</div>');
		$('.close', uiDialogTitlebar)
			.mousedown(function(ev) {
				ev.stopPropagation();
			})
			.click(function() {
				self.close();
				return false;
			});
	
		if (options.draggable) {
			uiDialog.makeDraggable();
		}
	
		this.open = function() {
			if(options.overlay){
				$("#overlay").show();
			}
			uiDialog.appendTo('body').show();

			// CALLBACK: open
			var openEV = null;
			var openUI = {
				options: options
			};
			$(this.element).triggerHandler("dialogopen", [openEV, openUI], options.open);
		};

		this.close = function() {
			uiDialog.hide();
			if(options.overlay){
				$("#overlay").hide();
			}
			// CALLBACK: close
			var closeEV = null;
			var closeUI = {
				options: options
			};
			$(this.element).triggerHandler("dialogclose", [closeEV, closeUI], options.close);
		};

	}

	$.ui.dialogOpen = function(el) {
		$.data(el, "ui-dialog").open();
	}

	$.ui.dialogClose = function(el) {
		$.data(el, "ui-dialog").close();
	}

})(jQuery);
